﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom.Compiler;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using Tedds_Tool.Compilers.CSharp;
using LEET.Application.Compilers.CSharp;
using System.Windows;

namespace Tedds_Tool
{
    public class CsCompilerResult : ICsCompilerResult
    {
        private CompilerResults result;
        private StringBuilder msg;

        public CsCompilerResult(CompilerResults cr)
        {
            result = cr;
            msg = new StringBuilder(string.Empty);
        }

        public string ExecutingDirectory
        {
            get
            {
                if (HasExecutable == true)
                    return Path.GetDirectoryName(result.PathToAssembly);
                else
                    return string.Empty;
            }
        }

        public bool HasExecutable
        {
            get
            {
                return File.Exists(result.PathToAssembly);
            }
        }

        public Process RunExecutable()
        {
            new FileInfo(result.PathToAssembly).Directory.Create();

            ProcessStartInfo info = new ProcessStartInfo(result.PathToAssembly);
            info.RedirectStandardError = true;
            info.RedirectStandardOutput = true;
            info.UseShellExecute = false;
            info.CreateNoWindow = true;
            
            Process process = new Process();
            process.StartInfo = info;
            
            return process;
        }

        public bool HasErrors
        {
            get { return result.Errors.Count > 0; }
        }

        public IEnumerable<ICsCompilerError> Errors
        {
            get
            {
                foreach (CompilerError err in result.Errors)
                    yield return new CsCompilerError(err);
            }
        }

        public override string ToString()
        {
            StringBuilder buf = new StringBuilder();
            foreach (CsCompilerError error in Errors)
                buf.Append(error.ToString()).Append("\r\n");
            return buf.ToString();
        }
    }
}
